//
//  HOTAddSceneViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-9.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTAddGenbaViewController.h"
#import "ZZZUIScrollView+Extends.h"
#import "UITextField+extend.h"

@interface HOTAddGenbaViewController () {
    
    __unsafe_unretained HOTAddGenbaViewController *unsafeObj;
}
@property(nonatomic,copy)NSString *dist_id;
@property(nonatomic,strong)NSMutableArray *compenyModelList;
@property(nonatomic,copy)NSString *company_id;
@end

@implementation HOTAddGenbaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNaviTitle:@"現場名追加"];
    
    unsafeObj = self;
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_dump"]];
    
    [self initView];
    
    [self requestCompanyDataFromNet];
}

-(void)initView {
    
    NSArray *modelList = [HOTUtils getPullModelListFromPlist];
    _regionPullDownView.pullDownModelList = modelList;
    _regionPullDownView.parentView = _mainScrollView;
    _regionPullDownView.SelectedComplete = ^(ZZZPullDownModel *model) {
        
        unsafeObj.dist_id = model.value;
        
        [self.compenyModelList removeAllObjects];
        [unsafeObj showLoading];
        [unsafeObj requestCompanyDataFromNet];

    };
    [_regionPullDownView setSelectedModel:[modelList firstObject]];
    
    _compenyPullDownView.parentView = _mainScrollView;
    self.compenyModelList = [NSMutableArray array];
}

/**
 *  获取公司列表
 */
-(void)requestCompanyDataFromNet {
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_CITY_ID:[HOTUtils getString:self.dist_id],
                                 };
    [self requestWithURL:HOT_URL_COMPANY_LIST Parameters:parameters success:^(NSInteger result, NSArray * dataArr) {
        
        [unsafeObj loadingSuccess];
        //空值
        ZZZPullDownModel *model = [[ZZZPullDownModel alloc] init];
        model.value = @"";
        model.titleStr = @"指定しない";

        [self.compenyModelList addObject:model];
        for (NSDictionary *dic in dataArr) {
            
            ZZZPullDownModel *model = [[ZZZPullDownModel alloc] init];
            model.value = [dic objectForKey:@"company_id"];
            model.titleStr = [dic objectForKey:@"company_name"];
            
            [self.compenyModelList addObject:model];
        }
        _compenyPullDownView.SelectedComplete = ^(ZZZPullDownModel *model) {
            
            unsafeObj.company_id = model.value;
        };
        _compenyPullDownView.pullDownModelList = self.compenyModelList;
        [_compenyPullDownView setSelectedModel:[self.compenyModelList firstObject]];
        
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [unsafeObj loadingSuccess];
        
        if ([super checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
    
}

-(void)saveDataToNet {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_DIST_ID:self.dist_id,
                                 REQ_KEY_COMPENY_ID:[HOTUtils getString:self.company_id],
                                 REQ_KEY_WORKP_NAME:[HOTUtils getString:_workPlaceText.text],
                                 };
    
    //__unsafe_unretained HOTCloseDownDetailListViewController *tempObj = self;
    [self requestWithURL:HOT_URL_MOCK_ADD Parameters:parameters success:^(NSInteger result, NSDictionary * dataArr) {

        [self showSuccessWithMessage:MSG_SUCCESS complection:^{
            
            [unsafeObj.navigationController popViewControllerAnimated:YES];
        }];

        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [unsafeObj loadingSuccess];
        if ([unsafeObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [_mainScrollView scrollViewToVisible:textField animated:YES];
}

-(BOOL)checkParameters {
    
    if ([_workPlaceText textIsNullOrEmpty]) {
        
        NSString *errMsg = [self getErrMsgWith:@"現場名" itemType:HOTInputItemText];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:errMsg
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        return NO;
    }
    
    return YES;
}

- (IBAction)sendBtnClicked:(id)sender {
    
    if (![self checkParameters]) {
        return;
    }
    
    [self saveDataToNet];
}
@end
